<?php
include('connect_db.php');
/*if logged in, redirect to homepage */
if (!checkLoggedin())
{
	header('Location: login.php');
}

/* admin account goes to admin page */
if($_SESSION['privilege'] == 1)
{
	header('Location: admin.php');
}

$sql = "SELECT firstname, lastname, id FROM users WHERE id IN ( SELECT H2.user_id AS id FROM houses_users H1, houses_users H2 WHERE H1.house_id = H2.house_id AND H1.user_id = " . $_SESSION['id'] . " AND H2.user_id <> " . $_SESSION['id'] . ")";
$friends = mysql_query( $sql );

?>
<?php include('templates/main/header.php'); ?>

    <div id="body">
		<div id="content" class="right_sidebar">
			<div id="main-page">
			<hr>
			<h1>Welcome, <?php echo $_SESSION['firstname'] . ' ' . $_SESSION['lastname']; ?>!</h1>
			<hr>
			<br />
			  <?php 
			  while ( $row = mysql_fetch_array($friends) ) 
			  {
			    $sql = "SELECT SUM(B.amount*BU.shared_scale) AS sum FROM bills B, bills_users BU
	                    WHERE BU.user_id = " . $_SESSION['id'] . "
	                    AND if_paid != True
	                    AND BU.bill_id = B.id
	                    AND B.user_id =" . $row['id'];
	            $i_owe_sum = mysql_fetch_row( mysql_query($sql) );

	            $sql = "SELECT SUM(B.amount*BU.shared_scale) AS sum FROM bills B, bills_users BU
	                    WHERE BU.user_id = " . $row['id'] . "
	                    AND if_paid != True
	                    AND BU.bill_id = B.id
	                    AND B.user_id =" . $_SESSION['id'];
	            $friend_owe_sum = mysql_fetch_row( mysql_query( $sql ) );

				$sql = "SELECT username FROM users WHERE id = " . $row['id'];
				$user_info = mysql_fetch_row( mysql_query( $sql ) );
						?><br/>
				<p><img border="1px" src="uploads/<?php
				        if (file_exists("uploads/" . md5($row['id'].$user_info[0]).'.png'))
		         	    {
				          echo  md5($row['id'].$user_info[0]);
		         	    }
		         	    else
		         	    {
				          echo 'zombie';
		         	    }
						$net = ( round($i_owe_sum[0], 2) - round($friend_owe_sum[0], 2) );
			        ?>.png" width="98" height="98"></p>
				<?php if ($net < 0) { ?>
				<div id="net-clear">
					<a class="clear-debts" href="clear.php?id=<?php echo $row['id'];?>" onClick="return HTMerDel();">Clear!</a>
					<p class="net-total <?php if ($net < 0) echo 'you-owe'; else echo 'i-owe'; ?>">$<?php echo abs($net); ?></p>
				</div>
				<?php } else { ?>
					<p class="net-total <?php if ($net < 0) echo 'you-owe'; else echo 'i-owe'; ?>">$<?php echo abs($net); ?></p>
				<?php } ?>
				<p class="name"><?php echo $row['firstname'] . " " . $row['lastname'] ?></p>
				<p class="amount">I owe <span class="i-owe">$<?php echo round($i_owe_sum[0], 1) ?></span> - <span class="you-owe">$<?php echo round($friend_owe_sum[0], 1) ?></span> =</p>
				<br>
				<hr>
			  <?php
			  } ?>
			</div>
		</div>

    <script language="JavaScript" type="text/JavaScript">
	    function HTMerDel()
	    {
		    if(confirm("Are you sure you want clear bill with this person?"))
		    	return true;
		    else
		    	return false;
	    }
    </script>


		<?php include( 'sidebar.php' ); ?>
	</div>

<?php include('templates/main/footer.php'); ?>